<template>
<div class="goodss">
    <!-- //面包屑 -->
     <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加商品</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
       <el-alert title="添加商品信息" type="info" center  show-icon :closable="false"></el-alert>
    </el-card>
    <!-- //步骤条 -->
    <el-steps :active="active-0" finish-status="success" align-center>
            <el-step title="基本信息"></el-step>
            <el-step title="商品参数"></el-step>
            <el-step title="商品属性"></el-step>
            <el-step title="商品图片"></el-step>
            <el-step title="商品内容"></el-step>
            <el-step title="完成"></el-step>
    </el-steps>

    <!-- tab栏 -->
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-tabs v-model="active" :tab-position="'left'" :before-leave="beforeTabLeave"  @tab-click="getList">
                <el-tab-pane label="基本信息" name="0">
                     <el-form-item label="商品名称" prop="goods_name">
                            <el-input v-model="ruleForm.goods_name"></el-input>
                     </el-form-item>
                       <el-form-item label="商品价格" prop="goods_price">
                            <el-input v-model="ruleForm.goods_price" type="number"></el-input>
                        </el-form-item>
                    <el-form-item label="商品重量" prop="goods_weight">
                             <el-input v-model="ruleForm.goods_weight" type="number"></el-input>
                    </el-form-item>
                        <el-form-item label="商品数量" prop="goods_number">
                                <el-input v-model="ruleForm.goods_number" type="number"></el-input>
                        </el-form-item>
                         <el-form-item label="商品分类" prop="goods_cat">
                                  <el-cascader clearable expandTrigger="hover" v-model="ruleForm.goods_cat" :options="list"
                                        :props="cateProps" @change="handleChange">
                                  </el-cascader>
                        </el-form-item>
                    
                </el-tab-pane>
           
                <el-tab-pane label="商品参数" name="1">
                    <el-form-item :label="item.attr_name" v-for="(item,index) in manyTableData" :key="index">
                            <el-checkbox-group v-model="item.attr_vals">
                       <el-checkbox :label="cb" v-for="(cb,i) in item.attr_vals" :key="i" border></el-checkbox></el-checkbox-group>
                     </el-form-item>
                </el-tab-pane>
                <el-tab-pane label="商品属性" name="2">
                    <el-form-item :label="item.attr_name" v-for="(item,index) in onlyTableData" :key="index">
                         <el-input v-model="item.attr_vals"></el-input>
                     </el-form-item>
                </el-tab-pane>
                <el-tab-pane label="商品图片" name="3">
                    <el-upload :action="img" 
                    :on-preview="handlePreview" 
                    :on-remove="handleRemove"
                     list-type="picture"
                     :headers="headerobj"
                     :on-success="handleSuccess"
>
  <el-button size="small" type="primary" >点击上传</el-button>
</el-upload>
                </el-tab-pane>
              <el-tab-pane label="商品内容" name="4">
               <!-- 富文本编辑器 -->
               <quill-editor v-model="ruleForm.goods_introduce"></quill-editor>
               <!-- 添加商品 -->
               <el-button type="primary" class="btnAdd" @click="add">添加商品</el-button>
             </el-tab-pane>
         </el-tabs>
    </el-form>

<el-dialog
  title="提示"
  :visible.sync="dialogVisible"
  width="300px">
  <img :src="tu" alt="" class="img1">
  
</el-dialog>

    

</div>
</template>
<script>
import { getsp, tableData, getcan, getonly } from "@/api/api.js";
export default {
  name: "goodss",
  props: [],
  data() {
    return {
      active: "0",

      headerobj: {
        Authorization: localStorage.getItem("token")
      },
      manyTableData: [], //动态参数
      onlyTableData: [], //静态参数
      list: [],
      img: "https://lianghj.top:8888/api/private/v1/upload",
      //   selectedCateKeys: [],
      tabPosition: "left",
      tu: "", //预览图片
      dialogVisible: false,
      ruleForm: {
        //表单对象
        goods_name: "",
        goods_price: 0,
        goods_weight: 0,
        goods_number: 0,
        goods_cat: [],
        pics: [],
        goods_introduce:'',
        attrs:[]
      },
      cateProps: {
        //商品分类列表
        value: "cat_id",
        label: "cat_name",
        children: "children"
      },
      rules: {
        goods_name: [
          { required: true, message: "请输入活动名称", trigger: "blur" }
        ],
        goods_price: [
          { required: true, message: "请输入活动名称", trigger: "blur" }
        ],
        goods_weight: [
          { required: true, message: "请输入活动名称", trigger: "blur" }
        ],
        goods_number: [
          { required: true, message: "请输入活动名称", trigger: "blur" }
        ],
        goods_cat: [
          { required: true, message: "请输入活动名称", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    //钩子函数实例创建之后
    this.xuan();
  },
  methods: {
    getList() {
      //   console.log(this.active);
      if (this.active === "1") {
        // console.log("动态参数");
        getcan({ cat_id: this.ruleForm.goods_cat[2] }).then(res => {
          res.data.forEach(item => {
            item.attr_vals =
              item.attr_vals.length === 0 ? [] : item.attr_vals.split(" ");
          });
          this.manyTableData = res.data;

          console.log(res.data);
        });
        // console.log(this.ruleForm.goods_cat[2]);
      } else if (this.active === "2") {
        getonly({ cat_id: this.ruleForm.goods_cat[2] }).then(res => {
          this.onlyTableData = res.data;

          console.log(res.data);
        });
      }
    },
    //点击图片名称触发函数
    handlePreview() {},
    //移除图片触发函数
    handleRemove(file) {},

    //方法
    xuan() {
      tableData(this.list).then(res => {
        if (res.meta.status === 200) {
          this.$message.success("获取商品分类列表成功");
          this.list = res.data;
          //   console.log(this.list);
        }
      });
    },
    //级联选择器触发
    handleChange() {
      // console.log(this.ruleForm.goods_cat);
      if (this.ruleForm.goods_cat.length !== 3) {
        this.ruleForm.goods_cat = [];
      }
    },
    beforeTabLeave(activeName, oldActiveName) {
      console.log(activeName, oldActiveName); //新的，老的
      //判断条件-从第1个离开同时下拉列表选择了第3个
      if (oldActiveName == 0 && this.ruleForm.goods_cat.length != 3) {
        this.$message.error("请选择商品分类为3级");
      }
    },
    //点击预览
    handlePreview(file) {
      console.log(file);
      this.tu = file.response.data.url;
      this.dialogVisible = true;
    },
    //移除照片
    handleRemove() {
      const filepath = file.response.data.tmp_path;
      const i = this.ruleForm.pics.findIndex(x => x.pic === filepath);
      this.ruleForm.pics.split(i, 1);
      console.log(this.ruleForm);
    },
    //图片上传成功触发的函数
    handleSuccess(response) {
      console.log(response);
    },
    add(){
      const form=JSON.parse(JSON.stringify(this.ruleForm))
      form.goods_cat=form.goods_cat.join(',')
      //处理动态参数
      this.manyTableData.forEach(item=>{
        const zxc={
            attr_id:item.attr_id, 
            attr_value:item.attr_vals.join(' ')
        }
        this.ruleForm.attrs.push(zxc)
      })
        this.onlyTableData.forEach(item=>{
        const zxc={
            attr_id:item.attr_id, 
            attr_value:item.attr_vals
        }
        this.ruleForm.attrs.push(zxc)
       
      })
      form.attrs=this.ruleForm.attrs
       getsp(form).then(res=>{
     
          if(res.meta.status===201){
            this.$router.push("/goods")
          }
        })
    }
  },

  components: {
    //注册子组件
  },
  watch: {
    //监听
  }
};
</script>
<style lang="scss" scoped>
.el-steps {
  margin-top: 30px;
}
.ql-editor{
  height: 300px;

}
.el-tabs {
  margin-top: 30px;
}
.el-checkbox {
  color: #fff;
  margin: 0 10px 0 0 !important;
}

.img1 {
  width: 100px;
  height: 100px;
  margin: 0 auto;
}
.a1 {
  width: 100px;
  height: 30px;
  line-height: 30px;
  color: #fff;
  float: left;
  margin-left: 20px;
  background-color: rgb(162, 162, 238);
}
</style>